<?php

namespace Home\Controller;

class VideoController extends BaseController {

    public function _initialize() {
        //重写，勿动
    }

    public function getList() {
        $p = I('p', 0, 'intval');

        if ($p) {
            $page = $p * 10;
        }
        $where['is_del'] = 0;

        $list = M('Video')
                ->field('video_id,video_url,video_content,img_url,view_num,create_time,type')
                ->where($where)
                ->order('sort')
                ->limit($page, 10)
                ->select();

        if ($list) {
            foreach ($list as &$val) {
                $val['img_url'] = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $val['img_url'];

                if ($val['type'] == 0) {
                    $val['video_url'] = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $val['video_url'];
                }
                unset($val['type']);
            }
            unset($val);
        } else {
            $list = [];
        }

        $this->success('获取成功', $list);
    }

    public function detail() {
        $id = I('id', 0, 'intval');
        if ($id == 0) {
            $this->error('错误的ID');
        }

        $data = M('Video')->where(['video_id' => $id])->find();

        if (empty($data)) {
            if ($data['type'] == 0) {
                $data['video_url'] = 'http://' . $_SERVER['HTTP_HOST'] . __ROOT__ . $data['video_url'];
            }
        }

        $this->assign('data', $data);
        $this->display();
    }

}
